<template>
  <div class="alarmConfiguration zy-content">
    <search :columns="searchData" @getSearch="getSearch(arguments)"></search>
    <div class="list-layout mt16">
      <div class="btns clear">
        <span class="title">{{ $t("common.listInformation") }}</span>
        <Button @click="exportTemplateClick">{{
            $t("common.batchExport")
          }}
        </Button>
        <!-- <Button @click="getTableColumns(columns)">
          {{ handleContextMenuStatus ? $t("显示操作栏") : $t("隐藏操作栏") }}
        </Button> -->
      </div>
      <div class="tableContent">
        <Table
            class="resizable-table"
            border
            @on-column-width-resize="onColumnWidthResize"
            :columns="columnsList"
            ellipsis
            ref="selection"
            stripe
            :data="dataArr"
        >
        </Table>
      </div>
      <div class="pageWrap mt16">
        <pageLeftTitle :total="total" :selected="delIds.length"/>
        <Page
            :total="total"
            :page-size="pageSize"
            :page-size-opts="pageSizeOption"
            :current="pageNum"
            @on-change="changePageNum"
            @on-page-size-change="changePageSize"
            show-sizer
            show-total
        />
      </div>
    </div>
  </div>
</template>

<script>
import search from "@/components/curd/searchList.vue";
import fileDownload from "js-file-download";
import toggleTableOperation from "@/mixins/toggleTableOperation";
import {getAbnormalPhenomenaList} from "@/api/moldManagement/abnormalPhenomenaList.js";

const demoList = [
  {label: "试模NG", value: "1"},
  {label: "异常发生", value: "2"},
  {label: "冲次满保养", value: "3"},
  {label: "生产需要且完成任务", value: "4"},
];
export default {
  name: "benchmarkActualAnalysis",
  components: {search},
  mixins: [toggleTableOperation],
  data() {
    return {
      loading: false,
      searchParams: {},
      pageNum: 1,
      pageSize: 10,
      total: 0,
      delIds: [],
      urgencyLevelList: [
        {label: "特急", value: "1"},
        {label: "紧急", value: "2"},
        {label: "一般", value: "3"},
      ],
      searchData: [
        {
          title: "模具名称",
          key: "moldName",
        },
        {
          title: "模具编号",
          key: "moldCode",
        },
        {
          title: "是否新模",
          key: "acceptanceStatus",
          type: "select",
          dicData: [
            {label: "是", value: "1"},
            {label: "否", value: "0"},
          ],
        },
        {
          title: "紧急程度",
          key: "urgencyLevel",
          type: "select",
          dicData: [
            {label: "特急", value: "1"},
            {label: "紧急", value: "2"},
            {label: "一般", value: "3"},
          ],
        },
        {
          title: "异常现象",
          key: "abnormalPhenomena",
          type: "cascader",
          dicData: getAbnormalPhenomenaList(),
        },
        {
          title: "拆模类型",
          key: "demoldingType",
          type: "select",
          dicData: demoList,
          // dicUrl: "/dms/faultCategory/dictList?name=拆模类型",
          // params: {
          //   label: "value",
          //   value: "value"
          // }
        },
        {
          title: "部件",
          key: "component",
          type: "select",
          dicUrl: "/dms/faultCategory/dictList?name=部件",
          params: {
            label: "value",
            value: "value",
          },
        },
        {
          title: "维修人",
          key: "repairedByName",
          dicUrl: "/basic/user/searchUser",
          type: "search",
          queryName: "key",
          resultParams: "res.data",
          params: {
            label: "realname",
            value: "realname",
          },
        },
        {
          title: "故障发生时间",
          key: "faultTime",
          type: "datetimerange",
          format: "yyyy-MM-dd HH:mm:ss",
          span: 8,
        },
        {
          title: "拆模时间",
          key: "beginTime",
          type: "datetimerange",
          format: "yyyy-MM-dd HH:mm:ss",
          span: 8,
        },
        {
          title: "模具送达时间",
          key: "productReveiveTime",
          type: "datetimerange",
          format: "yyyy-MM-dd HH:mm:ss",
          span: 8,
        },
        {
          title: "模具收货时间",
          key: "receiveTime",
          type: "datetimerange",
          format: "yyyy-MM-dd HH:mm:ss",
          span: 8,
        },
        {
          title: "模具维修开始时间",
          key: "repairBeginTime",
          type: "datetimerange",
          format: "yyyy-MM-dd HH:mm:ss",
          span: 8,
        },
        {
          title: "模具维修结束时间",
          key: "repairCompleteTime",
          type: "datetimerange",
          format: "yyyy-MM-dd HH:mm:ss",
          span: 8,
        },
        {
          title: "验收类型",
          key: "acceptType",
          type: "select",
          dicData: [
            {label: "试模NG", value: "试模NG"},
            {label: "完成", value: "完成"},
          ],
        },
        {
          title: "验收时间",
          key: "acceptTimes",
          type: "datetimerange",
          format: "yyyy-MM-dd HH:mm:ss",
          span: 8,
        },
      ],
      columns: [
        {
          title: "送修单号",
          key: "repairOrderNo",
          minWidth: 130,
          align: "center",
          resizable: true,
        },
        {
          title: "模具名称",
          key: "moldName",
          minWidth: 130,
          align: "center",
          resizable: true,
        },
        {
          title: "模具编号",
          key: "moldCode",
          minWidth: 130,
          align: "center",
          resizable: true,
        },
        {
          title: "是否新模",
          key: "acceptanceStatus",
          minWidth: 150,
          align: "center",
          resizable: true,
          render: (h, params) => {
            return h("div", [
              h("span", params.row.acceptanceStatus == "1" ? "是" : "否"),
            ]);
          },
        },
        {
          title: "紧急程度",
          key: "urgencyLevel",
          minWidth: 150,
          align: "center",
          resizable: true,
          render: (h, params) => {
            return h("div", [
              h(
                  "span",
                  this.urgencyLevelList.find(
                      (item) => item.value == params.row.urgencyLevel
                  ).label
              ),
            ]);
          },
        },
        {
          title: "拆模类型",
          key: "demoldingType",
          minWidth: 150,
          align: "center",
          resizable: true,
          render: (h, params) => {
            return h("div", [
              h(
                  "span",
                  params.row.demoldingType ? demoList.find(
                      (item) => item.value == params.row.demoldingType
                  ).label : '--'
              ),
            ]);
          },
        },

        {
          title: "故障原因",
          key: "faultReason",
          minWidth: 150,
          align: "center",
          resizable: true,
        },
        {
          title: "异常现象",
          key: "abnormalPhenomena",
          minWidth: 150,
          align: "center",
          resizable: true,
        },
        {
          title: "故障类别",
          key: "faultTypeList",
          minWidth: 150,
          align: "center",
          resizable: true,
        },
        {
          title: "故障部件",
          key: "faultPart",
          minWidth: 150,
          align: "center",
          resizable: true,
        },
        {
          title: "维修处置内容",
          key: "repairContent",
          minWidth: 180,
          align: "center",
          resizable: true,
        },
        {
          title: "报修人",
          key: "repairApplicant",
          minWidth: 150,
          align: "center",
          resizable: true,
        },
        {
          title: "维修人",
          key: "repairedByName",
          minWidth: 150,
          align: "center",
          resizable: true,
        },
        {
          title: "故障发生时间",
          key: "faultTime",
          minWidth: 130,
          align: "center",
          resizable: true,
        },
        {
          title: "拆模时间",
          key: "beginTime",
          minWidth: 130,
          align: "center",
          resizable: true,
        },
        {
          title: "模具送达时间",
          key: "productReveiveTime",
          minWidth: 130,
          align: "center",
          resizable: true,
        },
        {
          title: "模具收货时间",
          key: "receiveTime",
          minWidth: 130,
          align: "center",
          resizable: true,
        },
        {
          title: "开始维修时间",
          key: "repairBeginTime",
          minWidth: 130,
          align: "center",
          resizable: true,
        },
        {
          title: "结束维修时间",
          key: "repairCompleteTime",
          minWidth: 130,
          align: "center",
          resizable: true,
        },
        {
          title: "维修总时长(min)",
          key: "maintainTime",
          minWidth: 130,
          align: "center",
          resizable: true,
        },
        {
          title: "（代装）生产停止时间",
          key: "producedQuantity",
          minWidth: 130,
          align: "center",
          resizable: true,
        },
        {
          title: "验收类型",
          key: "acceptType",
          minWidth: 150,
          align: "center",
          resizable: true,
        },
        {
          title: "验收时间",
          key: "acceptTime",
          minWidth: 130,
          align: "center",
          resizable: true,
        },
        // {
        //   title: "最终审核时间",
        //   key: "acceptTime",
        //   minWidth: 130,
        //   align: "center",
        //   resizable: true,
        // },
      ],
      dataArr: [],
      pageSizeOption: [10, 20, 30, 40],
    };
  },
  mounted() {
    this.getTableColumnsWidth(this.columns);
    this.searchList();
  },
  methods: {
    getSearch(arg) {
      this.searchParams = JSON.parse(JSON.stringify(arg[0]));
      const {
        faultTime,
        beginTime,
        productReveiveTime,
        receiveTime,
        repairBeginTime,
        repairCompleteTime,
        abnormalPhenomena,
        acceptTimes
      } = this.searchParams;
      // 故障时间
      if (faultTime && faultTime.length > 0) {
        this.searchParams.faultBeginTime = this.searchParams.faultTime[0];
        this.searchParams.faultEndTime = this.searchParams.faultTime[1];
        delete this.searchParams.faultTime;
      }
      // 拆模时间
      if (beginTime && beginTime.length > 0) {
        this.searchParams.beginBeginTime = this.searchParams.beginTime[0];
        this.searchParams.beginEndTime = this.searchParams.beginTime[1];
        delete this.searchParams.beginTime;
      }
      // 模具送达时间
      if (productReveiveTime && productReveiveTime.length > 0) {
        this.searchParams.productReveiveBeginTime =
            this.searchParams.productReveiveTime[0];
        this.searchParams.productReveiveEndTime =
            this.searchParams.productReveiveTime[1];
        delete this.searchParams.productReveiveTime;
      }
      // 模具收获时间
      if (receiveTime && receiveTime.length > 0) {
        this.searchParams.receiveBeginTime = this.searchParams.receiveTime[0];
        this.searchParams.receiveEndTime = this.searchParams.receiveTime[1];
        delete this.searchParams.receiveTime;
      }
      // 验收时间
      if (acceptTimes && acceptTimes.length > 0) {
        this.searchParams.acceptStartTime = this.searchParams.acceptTimes[0];
        this.searchParams.acceptEndTime = this.searchParams.acceptTimes[1];
        delete this.searchParams.acceptTimes;
      }
      //模具开始维修时间
      if (repairBeginTime && repairBeginTime.length > 0) {
        this.searchParams.repairBeginBeginTime =
            this.searchParams.repairBeginTime[0];
        this.searchParams.repairBeginEndTime =
            this.searchParams.repairBeginTime[1];
        delete this.searchParams.repairBeginTime;
      }
      //模具结束维修时间
      if (repairCompleteTime && repairCompleteTime.length > 0) {
        this.searchParams.repairCompleteBeginTime =
            this.searchParams.repairCompleteTime[0];
        this.searchParams.repairCompleteEndTime =
            this.searchParams.repairCompleteTime[1];
        delete this.searchParams.repairCompleteTime;
      }
      if (abnormalPhenomena && abnormalPhenomena.length > 0) {
        this.searchParams.abnormalPhenomena = abnormalPhenomena.at(-1);
      } else delete this.searchParams.abnormalPhenomena;
      this.pageNum = 1;
      this.searchList();
    },
    reset() {
      this.searchParams = {};
      this.changeNum();
    },
    //分页
    changeNum() {
      this.pageNum = 1;
      this.searchList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.searchList();
    },

    changePageNum(val) {
      this.pageNum = val;
      this.searchList();
    },
    //查询列表
    searchList() {
      let params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        ...this.searchParams,
      };
      this.axios({
        method: "post",
        url: "/dms/moldMaintenanceHall/queryMoldMaintainHallList",
        data: params,
      }).then((res) => {
        if (res.code === 200) {
          this.dataArr = res.data.records;
          this.total = res.data.total;
        } else {
          this.$Message.warning({
            content: res.msg,
            duration: 6,
            closable: true,
          });
        }
      });
    },
    //导出
    exportTemplateClick() {
      let params = {
        delIds: this.delIds,
      };
      for (let key in this.searchParams) {
        if (this.searchParams[key]) {
          this.$set(params, key, this.searchParams[key]);
        }
      }
      const filName = "模具维修报表";
      this.axios({
        url: "/dms/moldMaintenanceHall/exportMoldMaintainHallList",
        method: "post",
        responseType: "blob",
        data: params,
      }).then((res) => {
        fileDownload(res, filName + ".xls");
      });
    },
  },
};
</script>

<style scoped lang="scss">
.ivu-input-number {
  width: -webkit-fill-available;
}

.row_click {
  cursor: pointer;
  color: rgb(50, 113, 240) !important;
}
</style>
